package com.rhs.controller;

import com.rhs.domain.User;
import com.rhs.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Arrays;
import java.util.List;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/list")
    public String index(Model model, User user) {

        List<User> users = userService.selectByWhere(user);
        model.addAttribute("users", users);
        return "user-list";
    }

    @RequestMapping("/addUI")
    public String addUI() {
        return "user-add";
    }

    @RequestMapping("/add")
    public String add(User user) {
        userService.insert(user);
        return "redirect:/list";
    }

    @RequestMapping("/delete")
    public String delete(int uid) {
        userService.delete(uid);
        return "redirect:/list";
    }

    @RequestMapping("/updateUI")
    public String updateUI(int uid, Model model) {
        User user = userService.selectById(uid);
        model.addAttribute("user", user);
        return "user-update";
    }

    @RequestMapping("/update")
    public String update(User user) {
        userService.update(user);
        return "redirect:/list";
    }

    @RequestMapping("/deleteBatch")
    public String deleteByIds(int[] ids) {
        System.out.println(Arrays.toString(ids));
        userService.deleteByIds(ids);
        return "redirect:/list";
    }

    @RequestMapping("/test")
    public String test(Model model, User user) {
        List<User> users = userService.selectByWhere(user);
        model.addAttribute("users", users);
        return "test";
    }
}
